package com.action.school;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.action.base.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.pageModel.Grade;
import com.pageModel.Json;
import com.service.school.GradeServiceI;

/**
 * 年级ACTION
 * 
 * @author chenq
 * 
 */

@Namespace("/school")
@Action(value = "gradeAction")
public class GradeAction extends BaseAction implements ModelDriven<Grade>{

	Grade grade=new Grade();
	
	@Override
	public Grade getModel() {
		return grade;
	}
	
	private GradeServiceI gradeService;

	public GradeServiceI getGradeService() {
		return gradeService;
	}
	@Autowired
	public void setGradeService(GradeServiceI gradeService) {
		this.gradeService = gradeService;
	}
	
	public void getGradeCmb(){
		super.writeJson(gradeService.getGradeCmb(grade));
	}
	
	public void datagrid(){
		super.writeJson(gradeService.datagrid(grade));
	}

	public void remove(){
		gradeService.remove(grade.getIds());
		Json j = new Json();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}
	
	public void add(){
		String json = null;
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			boolean state=gradeService.add(grade);
			if (state) {
				m.put("success", true);
				m.put("msg", "添加成功!");
			} else {
				m.put("success", false);
				m.put("msg", "该年级已存在!");
			}
			m.put("success", true);
			m.put("msg", "添加成功!");
		} catch (Exception e) {
			e.printStackTrace();
			m.put("success", false);
			m.put("msg", "添加失败！");
			System.out.print(e.getMessage());
		}
		super.writeJson(m);
	}
	
	public void edit(){
		gradeService.edit(grade);
		Json j=new Json();
		j.setSuccess(true);
		j.setMsg("修改成功！");
		super.writeJson(j);
	}
}
